package com.nuoniu.sibanyun.controller.msg;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.msg.SysMsg;
import com.nuoniu.sibanyun.service.msg.ISysMsgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 消息管理 前端控制器
 * </p>
 *
 * @author 小啊刚
 * @since 2022-01-07
 */
@RestController
@RequestMapping("/sys/msg")
@Api(tags = "消息管理")
public class SysMsgController extends BaseController {

    @Autowired
    private ISysMsgService msgService;

    @ApiOperation("获取消息数量")
    @GetMapping("getMsgCount")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型(默认所有) 0:系统1客户2:采购3:销售4:库存5:生产6:财务"),
            @ApiImplicitParam(name = "status", value = "状态(默认所有): 0未读 1已读")
    })
    public HttpResult getMsgCount(Integer type, Integer status, String userId) {
        return HttpResult.ok(msgService.getMsgCount(type, status));
    }

    @ApiOperation("获取消息列表")
    @GetMapping("pageList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型(默认所有) 0:系统1客户2:采购3:销售4:库存5:生产6:财务"),
            @ApiImplicitParam(name = "status", value = "状态(默认所有): 0未读 1已读")
    })
    public HttpResult<SysMsg> pageList(Page page, Integer type, Integer status) {
        return HttpResult.ok(msgService.pageList(page, type, status));
    }

    @ApiOperation("阅读消息")
    @PostMapping("readMsg")
    public HttpResult readMsg(@RequestBody List<Long> ids) {
        if(ids.size() == 0) {
            throw new NuoNiuException("消息不存在");
        }

        msgService.readMsg(ids);
        return HttpResult.ok();
    }

    @ApiOperation("全部标记已读")
    @PostMapping("realAll")
    public HttpResult realAll() {
        msgService.realAll();
        return HttpResult.ok();
    }

    @ApiOperation("删除消息")
    @PostMapping("delete")
    public HttpResult delete(@RequestBody List<Long> ids) {
        if(ids == null || ids.size() == 0) {
            throw new NuoNiuException("请选择要删除的消息");
        }

        msgService.delete(ids);
        return HttpResult.ok();
    }
}
